package view.swinginterface;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Map;

import static view.swinginterface.LoginForm.checkTextField;

/**
 * @author Innokenty Shuvalov
 */
public class SearchUserForm extends JDialog {
    private SwingInterface owner;

    public SearchUserForm(SwingInterface owner) {
        super(owner);
        initComponents();

        this.owner = owner;
    }

    private DefaultListModel listModel;

    public void setSearchResults(Map<String, Integer> searchResults) {
        listModel = new DefaultListModel();
        searchResultsList.setModel(listModel);
        for (Map.Entry<String, Integer> entry : searchResults.entrySet()) {
            listModel.addElement(new UserListItem(entry.getKey(), entry.getValue()));
        }
        searchResultsList.setEnabled(true);
    }

    private void cancelButtonActionPerformed(ActionEvent e) {
        this.setVisible(false);
    }

    private void clearButtonActionPerformed(ActionEvent e) {
        nameTextField.setText("");
    }

    private void searchButtonActionPerformed(ActionEvent e) {
        if (checkTextField(nameTextField)) {
            owner.getRequestWorker().searchUser(nameTextField.getText().trim());
        }
    }

    private void inputFieldFocusGained(FocusEvent e) {
        ((JTextField) e.getComponent()).setBackground(Color.WHITE);
    }

    private void addButtonActionPerformed(ActionEvent e) {
        UserListItem user = (UserListItem) searchResultsList.getSelectedValue();
        owner.getRequestWorker().addBuddyToContacts(user.getUserId());
        clearButtonActionPerformed(null);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - iuygi iugiu
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        nameTextField = new JTextField();
        scrollPane1 = new JScrollPane();
        searchResultsList = new JList();
        buttonBar = new JPanel();
        searchButton = new JButton();
        addButton = new JButton();
        clearButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("Serach User");
        setMinimumSize(new Dimension(380, 340));
        setResizable(false);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setPreferredSize(new Dimension(300, 310));
            dialogPane.setMinimumSize(new Dimension(300, 310));
            dialogPane.setMaximumSize(new Dimension(300, 310));

            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setPreferredSize(new Dimension(360, 240));

                //---- label1 ----
                label1.setText("type user name");

                //---- nameTextField ----
                nameTextField.addFocusListener(new FocusAdapter() {
                    @Override
                    public void focusGained(FocusEvent e) {
                        inputFieldFocusGained(e);
                    }
                });

                //======== scrollPane1 ========
                {

                    //---- searchResultsList ----
                    searchResultsList.setEnabled(false);
                    scrollPane1.setViewportView(searchResultsList);
                }

                GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
                contentPanel.setLayout(contentPanelLayout);
                contentPanelLayout.setHorizontalGroup(
                        contentPanelLayout.createParallelGroup()
                                .addGroup(contentPanelLayout.createSequentialGroup()
                                        .addComponent(label1)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(nameTextField, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE))
                                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                );
                contentPanelLayout.setVerticalGroup(
                        contentPanelLayout.createParallelGroup()
                                .addGroup(contentPanelLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                .addComponent(label1)
                                                .addComponent(nameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                        .addContainerGap())
                );
            }
            dialogPane.add(contentPanel, BorderLayout.NORTH);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new FlowLayout());

                //---- searchButton ----
                searchButton.setText("Search");
                searchButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        searchButtonActionPerformed(e);
                    }
                });
                buttonBar.add(searchButton);

                //---- addButton ----
                addButton.setText("Add");
                addButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        addButtonActionPerformed(e);
                    }
                });
                buttonBar.add(addButton);

                //---- clearButton ----
                clearButton.setText("Clear");
                clearButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        clearButtonActionPerformed(e);
                    }
                });
                buttonBar.add(clearButton);

                //---- cancelButton ----
                cancelButton.setText("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cancelButtonActionPerformed(e);
                    }
                });
                buttonBar.add(cancelButton);
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    public static void main(String[] args) {
        new SearchUserForm(null).setVisible(true);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - iuygi iugiu
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JTextField nameTextField;
    private JScrollPane scrollPane1;
    private JList searchResultsList;
    private JPanel buttonBar;
    private JButton searchButton;
    private JButton addButton;
    private JButton clearButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
